// pages/menu/menu.js
const ini = require('../../common/init/init.js')
const conf = require('../../common/conf/conf.js')
var Zan = require('../../bower_components/zanui-weapp/dist/index');
var wxUserInfo = wx.getStorageSync('wxUserInfo')
var userInfo = wx.getStorageSync('userInfo')
var app = getApp();
Page(Object.assign({}, Zan.Switch, {
  handleZanSwitchChange(e) {
    var componentId = e.componentId;
    var checked = e.checked;
    console.log(checked);
    if (checked == true) {
      this.setData({
        [`${componentId}.checked`]: false
      });
    } else {
      this.setData({
        [`${componentId}.checked`]: true
      });
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      headImg:wxUserInfo.avatarUrl,
      userName:wxUserInfo.nickName,
      vipEndTime:""
    },
    menuPage: ini.menuPage,
    adSet: {
      checked: false
    },
    bussinessAsk: {
      checked: true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!userInfo.vipEndTime){
      this.setData({
        'userInfo.vipEndTime':'暂未开通VIP'
      })
    }else{
      this.setData({
        'userInfo.vipEndTime':userInfo.vipEndTime + "到期"
      })
    }
    console.log(ini.menuPage.length)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchChange: function (e) {
    console.log(e)
    var targetId = e.target.id
    var menu = this.data.menuPage
    if (targetId == 'bussinessAsk') {
      // this.setData({
      //   'bussinessAsk.checked': true
      // })
      wx.request({
        url: conf.menuPage.bussinessAskUrl,
        data: {
          userInfo: app.globalData.userInfo,
          switchChecked: e.detail.value
        },
        method: "POST",
        success: function (res) {
          this.setData({
            'bussinessAsk.checked':res.checked
          })
        }
      })
    } else if (targetId == 'adSet') {
      wx.request({
        url: conf.menuPage.adSetUrl,
        data: {
          userInfo: app.globalData.userInfo,
          switchChecked: e.detail.value
        },
        method: "POST",
        success: function (res) {
          this.setData({
            'adSet.checked': res.checked
          })
        }
      })
    }
  }
}))